<?php
/**
 * Created by PhpStorm.
 * User: Marmot
 * Date: 2018/5/8
 * Time: 13:53
 */
//通用模型类
namespace app\admin\model;

use think\Model;

class Common extends Model
{
    //获取nav数据
    public function get_nav($where=''){
        $res = $this->table('d_nav')->order('nav_sort desc')->where($where)->select();
        return $this->nav_cate($res);
    }
    //nav数据处理
    public function nav_cate($child='',$nav_parent=0){
        $cate = '';
        $cate_arr = [];
        foreach ($child as $key => $value){
//          nav_parent_name 父级导航名称转换
            if ($value['nav_parent'] == 0){
                $value['nav_parent_name'] = '顶级导航';
            }else{
                $r = $this->table('d_nav')->field('nav_name')->where('id='.$value['nav_parent'])->find();
                $value['nav_parent_name'] = $r->getData('nav_name');
            }
            //父子导航层级转换
            if($value['nav_parent']==$nav_parent){
                $cate_arr[] = $value;
                $cate=$this->nav_cate($child,$value['id']);
                if(!empty($cate)){
                    $value['child']=$cate;
                }else{
                    $value['child']='';
                }
            }
        }
        return $cate_arr;
    }

}
